﻿using UnityEngine;
using UnityEngine.UI;

public class ResumePanel : UIBoxBase
{
    public Slider ContinueSlider;
    public GameObject ContinueObject;
    public GameObject ReviveObject;

    private bool countBegin = false;

    public override void ShowBox()
    {
        base.ShowBox();

        InitResumePanel();
    }

    private void OnEnable()
    {
        //EventManager2.RewardAdsPlaySuccess += OnRewardAdsPlaySuccess;
    }

    private void OnDisable()
    {
        //EventManager2.RewardAdsPlaySuccess -= OnRewardAdsPlaySuccess;
    }

    void Update()
    {
        if(countBegin)
        {
            ContinueSlider.value -= Time.deltaTime;

            if(ContinueSlider.value <= 0)
            {
                OnLeaveButtonClick();
            }
        }
    }

    private void InitResumePanel()
    {
        bool resumeEnable = false;

        /*if(AdsManager.instance.CanPlayRewardAds())
        {
            ContinueSlider.value = ContinueSlider.maxValue;
            ContinueObject.SetActive(true);
            countBegin = true;
            resumeEnable = true;
        }
        else
        {
            countBegin = false;
            ContinueObject.SetActive(false);
        }*/
        countBegin = false;
        ContinueObject.SetActive(false);
        if(!resumeEnable)
        {
            OnLeaveButtonClick();
        }
    }

    public void OnContinueButtonClick()
    {
        countBegin = false;

        //AdsManager.instance.PlayRewardAds(AdsType.Revive);
    }

    public void OnReviveButtonClick()
    {
        AudioController.Play("PanelOpen");
        countBegin = false;
        ContinueObject.SetActive(false);

        if(GameManager.instance.CurrentData.CoinNumber >= 200)
        {
            GameManager.instance.BuyResumeGame();
            HideBox();
        }
        else
        {
            UIManager.instance.ShowBox(UIBoxType.ShopPanel);
        }
    }

    public void OnLeaveButtonClick()
    {
        countBegin = false;
        HideBox();
        UIManager.instance.ShowBox(UIBoxType.FailPanel);
    }
    
    /*private void OnRewardAdsPlaySuccess(AdsType adsType)
    {
        if (adsType != AdsType.Revive) return;

        GameManager.instance.ResumeGame();
        HideBox();
        
    }*/

}
